package de.akabahn.ebd.control;

import java.io.PrintWriter;
import java.io.StringWriter;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import de.akabahn.ebd.control.auth.AbstractAuthenticator;
import de.akabahn.ebd.control.auth.AccessEnum;
import de.akabahn.ebd.control.auth.RemoteAuthenticator;

/**
 * @author Peter Eimann
 */
public class EBD_ChangePasswordActivity extends Activity {

	protected String userName;
	protected AccessEnum role;
	private AbstractAuthenticator authenticator;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

        Thread.setDefaultUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler() {
            @Override
            public void uncaughtException(Thread paramThread, Throwable exception) {
                StringWriter stackTrace = new StringWriter();
                exception.printStackTrace(new PrintWriter(stackTrace));

                StringBuilder errormessage = new StringBuilder(exception.getMessage());
            	while (exception.getCause() != null) {
            		exception = exception.getCause();
            		errormessage.append("\n\t\t- ").append(exception.getMessage());
            	}

                Intent loginIntent = new Intent(EBD_ChangePasswordActivity.this, EBD_LoginActivity.class);
                loginIntent.putExtra("de.akabahn.ebd.control.errormessage", errormessage.toString());
                loginIntent.putExtra("de.akabahn.ebd.control.stacktrace", stackTrace.toString());
                loginIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
                loginIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                startActivity(loginIntent);
                System.exit(0);
            }
        });

        setContentView(R.layout.changepassword);

		userName = getIntent().getExtras().getString("de.akabahn.ebd.control.userName");
		role = AccessEnum.valueOf(getIntent().getExtras().getString("de.akabahn.ebd.control.roleName"));
		authenticator = RemoteAuthenticator.getInstance();

		Button change = (Button) findViewById(R.id.changepassword_change_button);
		change.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				changePassword();
			}
		});
	}

	@Override
	public void onStart() {
		super.onStart();

		((EditText) findViewById(R.id.changepassword_oldpassword)).setText("");
		((EditText) findViewById(R.id.changepassword_newpassword)).setText("");
		((EditText) findViewById(R.id.changepassword_newpassword2)).setText("");
		((EditText) findViewById(R.id.changepassword_oldpassword)).requestFocus();

		TextView userInfo = (TextView)findViewById(R.id.changepasssord_userInfo);
		userInfo.setText(getResources().getString(R.string.userInfo, userName, role));
	}

	private void changePassword() {
		String oldPassword = ((EditText)findViewById(R.id.changepassword_oldpassword)).getEditableText().toString();
		String newPassword = ((EditText)findViewById(R.id.changepassword_newpassword)).getEditableText().toString();
		String newPassword2 = ((EditText)findViewById(R.id.changepassword_newpassword2)).getEditableText().toString();

		if (TextUtils.isEmpty(newPassword)) {
			Toast.makeText(EBD_ChangePasswordActivity.this,
					getResources().getString(R.string.changepassword_newPasswordEmpty), Toast.LENGTH_LONG).show();
		} else if (!newPassword.equals(newPassword2)) {
			Toast.makeText(EBD_ChangePasswordActivity.this,
					getResources().getString(R.string.changepassword_passwordsNotEqual), Toast.LENGTH_LONG).show();
		} else if (!authenticator.changePassword(userName, oldPassword, newPassword)) {
			Toast.makeText(EBD_ChangePasswordActivity.this,
					getResources().getString(R.string.changepassword_changeFailed), Toast.LENGTH_LONG).show();
		} else {
			Toast.makeText(EBD_ChangePasswordActivity.this,
					getResources().getString(R.string.changepassword_changeOk), Toast.LENGTH_SHORT).show();
			((EditText) findViewById(R.id.changepassword_oldpassword)).setText("");
			((EditText) findViewById(R.id.changepassword_newpassword)).setText("");
			((EditText) findViewById(R.id.changepassword_newpassword2)).setText("");
			((EditText) findViewById(R.id.changepassword_oldpassword)).requestFocus();
		}
	}
}